
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * Created by lenovo on 2018/4/1.
 */
public class Registered extends JFrame implements ActionListener {
    JPanel jp1, jp2, jp3, jp4, jp5,jp6;//面板
    JLabel jlb1, jlb2, jlb3, jlb4,tilte;//标签
    JButton sign, rest,back;//按钮
    JTextField jtf1, jtf2;//文本
    JPasswordField jpf1, jpf2;//密码

    public static void main(String[] args) {
        Registered registered = new Registered();
    }

    public Registered() {
        //创建面板
        jp1 = new JPanel();
        jp2 = new JPanel();
        jp3 = new JPanel();
        jp4 = new JPanel();
        jp5 = new JPanel();
        jp6 = new JPanel();
        jp6.setLayout(new FlowLayout(FlowLayout.CENTER,0,10));
        //创建标签
        jlb1 = new JLabel("昵       称");
        jlb2 = new JLabel("账       号");
        jlb3 = new JLabel("密       码");
        jlb4 = new JLabel("确 认 密 码");
        tilte = new JLabel("用户注册");
        tilte.setFont(new Font("黑体",Font.BOLD,20));

        //创建按钮
        sign = new JButton("注册");
        rest = new JButton("重置");
        back = new JButton("返回");
        sign.addActionListener(this);
        rest.addActionListener(this);
        back.addActionListener(this);
        //创建文本框
        jtf1 = new JTextField(30);
        jtf2 = new JTextField(30);
        //创建密码框
        jpf1 = new JPasswordField(30);
        jpf2 = new JPasswordField(30);

        //设置布局管理
        this.setLayout(new GridLayout(6, 1));//网格式布局

        //加入各个组件
        jp6.add(tilte);

        jp1.add(jlb1);
        jp1.add(jtf1);

        jp2.add(jlb2);
        jp2.add(jtf2);

        jp3.add(jlb3);
        jp3.add(jpf1);

        jp4.add(jlb4);
        jp4.add(jpf2);

        jp5.add(sign);
        jp5.add(rest);
        jp5.add(back);

        //加入到JFrame
        this.add(jp6);
        this.add(jp1);
        this.add(jp2);
        this.add(jp3);
        this.add(jp4);
        this.add(jp5);

        //设置窗体
        this.setTitle("用户注册");//窗体标签
        this.setSize(340, 280);//窗体大小
        this.setLocationRelativeTo(null);//在屏幕中间显示(居中显示)
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);//退出关闭JFrame
        this.setVisible(true);//显示窗体

        //锁定窗体
        this.setResizable(false);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand() == "注册") {
            if (!jpf1.getText().equals("") && !jtf1.getText().equals("") && !jtf2.getText().equals("") && !jpf2.getText().equals("")) {
                if (!jpf1.getText().equals(jpf2.getText())) {
                    JOptionPane.showMessageDialog(null, "请再次确认密码！", "提示消息", JOptionPane.WARNING_MESSAGE);
                } else {
                    User user = new User(jtf1.getText(), jtf2.getText(), jpf1.getText(), 0);
                     if (ExInform.RegCheck(user)) {
                        JOptionPane.showMessageDialog(null, "注册成功！", "提示消息", JOptionPane.WARNING_MESSAGE);
                        new Login();
                        this.dispose();
                    } else {
                        JOptionPane.showMessageDialog(null, "注册失败，用户名或账号已存在！", "提示消息", JOptionPane.WARNING_MESSAGE);
                        clear();
                    }
                }
            } else
                JOptionPane.showMessageDialog(null, "注册失败，注册信息不可为空！", "提示消息", JOptionPane.WARNING_MESSAGE);
        }
        if (e.getActionCommand() == "重置") {
            clear();
        }
        if(e.getActionCommand()=="返回"){
            new Login();
            this.dispose();
        }
    }

    private void clear() {
        jtf1.setText("");
        jtf2.setText("");
        jpf1.setText("");
        jpf2.setText("");
    }
}
